/**
 * 
 */
package com.m7012e.a_remote.communication.positioning;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author leoj
 *
 */
public class WifiRoughlyPosition extends Position {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2434141593923751412L;

	
	private Map<String, Integer> _routerMap;
	
	public WifiRoughlyPosition(){
		_routerMap = new HashMap<String, Integer>();
	}
	public void addRouter(String name, int signalStrength){
		_routerMap.put(name, signalStrength);
	}
	public WifiRoughlyPosition(Map<String, Integer> routerMap){
		_routerMap = routerMap;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(Position arg0) {
		if (arg0 instanceof WifiRoughlyPosition) {
			WifiRoughlyPosition otherPosition = (WifiRoughlyPosition) arg0;
			int compare = 0;
			Set<String> routersFromPos1 = this._routerMap.keySet();
			Set<String> routersFromPos2 = otherPosition._routerMap.keySet();
			
			for(String router : routersFromPos1){
				if(routersFromPos2.contains(router)){
					//router is common to the 2 positions
					System.out.println("found new common router : "+router);
					compare += Math.abs(otherPosition._routerMap.get(router) - this._routerMap.get(router));
					otherPosition._routerMap.remove(router);
				}else{
					//this router is only available in the current position ...
					compare += Math.abs(this._routerMap.get(router));
				}
			}
			
			Set<String> routersFromPos2Only = routersFromPos2;
			for(String router : routersFromPos2Only){
				compare += Math.abs(otherPosition._routerMap.get(router));
			}
			
			return compare;
		}else{
			throw new RuntimeException("this is not a WifiRoughlyPosition ! cannot compare !");
		}
	}

}
